<?php

/**
 * Created by PhpStorm.
 * User: boye009
 * Date: 2016/3/16
 * Time: 13:40
 */
namespace Bbs\Api;

use Bbs\Model\ForumPostcommentModel;
use Common\Api\Api;

class ForumPostcommentApi extends Api{

    /**
     * 添加
     */
    const ADD = "Bbs/ForumPostcomment/add";
    /**
     * 保存
     */
    const SAVE = "Bbs/ForumPostcomment/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "Bbs/ForumPostcomment/saveByID";

    /**
     * 删除
     */
    const DELETE = "Bbs/ForumPostcomment/delete";

    /**
     * 查询
     */
    const QUERY = "Bbs/ForumPostcomment/query";
    /**
     * 查询一条数据
     */
    const GET_INFO = "Bbs/ForumPostcomment/getInfo";

    const QUERY_WITH_COUNT = "Bbs/ForumPostcomment/queryWithCount";

    /**
     * 统计数量
     */
    const COUNT = "Bbs/ForumPostcomment/count";

    /**
     * 评论和帖子联表查询
     */
    const QUERY_WITH_POST = "Bbs/ForumPostcomment/queryWithPost";

    protected function _init(){
        $this -> model = new ForumPostcommentModel();
    }

    public function queryWithPost($map = null, $page = array('curpage'=>1,'size'=>6), $order = false, $params = false, $fields = false){

        $query = $this->model -> join("itboye_forum_post on itboye_forum_post.pid=itboye_forum_postcomment.pid");
        if(!is_null($map)){
            $query = $query->where($map);
        }
        if(!($order === false)){
            $query = $query->order($order);
        }
        if(!($fields === false)){
            $query = $query->field($fields);
        }
        $list = $query -> page($page['curpage'] . ',' . $page['size']) -> select();
        //return $this -> apiReturnSuc($list);


        if ($list === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count = $this -> model -> join("itboye_forum_post on itboye_forum_post.pid=itboye_forum_postcomment.pid") ->  where($map) -> count();

        // 查询满足要求的总记录数
        $Page = new \Think\Page($count, $page['size']);

        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }

        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();


        return $this -> apiReturnSuc(array("count" => $count, "list" => $list));

    }


}